from django import forms
from news.models import Tag, News
from doc.models import Doc
from course.models import Course


class NewsEditForm(forms.ModelForm):
    image_url = forms.URLField(label="文章图片URL", error_messages={
        "required": "文章图片URL不为空"
    })

    tag = forms.ModelChoiceField(queryset=Tag.objects.only("id").filter(is_delete=False), error_messages={
        "required": "文章ID不能为空"
    })

    class Meta:
        model = News
        # 指明字段
        fields = ["title", "digest", "content"]

        error_messages = {
            'title': {
                'max_length': '文章标题长度不能低于150',
                'min_length': '文章标题长度不能低于1',
                'required': '文章标题不能为空'
            },
            'digest': {
                'max_length': '文章摘要长度不能低于200',
                'min_length': '文章摘要长度不能低于1',
                'required': '文章摘要不能为空'
            },
            'content': {
                'required': '文本内容不能为空'
            },
        }


class NewsPubForm(forms.ModelForm):
    # 写在上面或下面都无所谓
    # 有特殊需求，需要验证
    image_url = forms.URLField(label="文章图片URL", error_messages={
        "required": "文章图片URL不为空"
    })

    tag = forms.ModelChoiceField(queryset=Tag.objects.only("id").filter(is_delete=False), error_messages={
        "required": "文章ID不能为空"
    })

    class Meta:
        model = News
        # 指明字段
        fields = ["title", "digest", "content", "tag", "image_url"]

        error_messages = {
            'title': {
                'max_length': '文章标题长度不能低于150',
                'min_length': '文章标题长度不能低于1',
                'required': '文章标题不能为空'
            },
            'digest': {
                'max_length': '文章摘要长度不能低于200',
                'min_length': '文章摘要长度不能低于1',
                'required': '文章摘要不能为空'
            },
            'content': {
                'required': '文本内容不能为空'
            },
        }


class DocEditForm(forms.ModelForm):
    image_url = forms.URLField(required=True, error_messages={
        "invalid": "不是一个地址",
        "required": "缩略图地址不能为空"
    })
    file_url = forms.URLField(required=True, error_messages={
        "invalid": "不是一个地址",
        "required": "缩略图地址不能为空"
    })

    class Meta:
        # 这里一般验证非特殊字段
        model = Doc
        fields = ["title", "desc", "file_url", "image_url"]
        error_messages = {
            'title': {
                'max_length': '文章标题长度不能低于150',
                'min_length': '文章标题长度不能低于1',
                'required': '文章标题不能为空'
            },
            'desc': {
                'max_length': '文章摘要长度不能低于200',
                'min_length': '文章摘要长度不能低于1',
                'required': '文章摘要不能为空'
            }
        }


class DocPubForm(forms.ModelForm):

    image_url = forms.URLField(required=True, error_messages={
        "invalid": "不是一个地址",
        "required": "缩略图地址不能为空"
    })
    file_url = forms.URLField(required=True, error_messages={
        "invalid": "不是一个地址",
        "required": "缩略图地址不能为空"
    })

    class Meta:
        # 这里一般验证非特殊字段
        model = Doc
        fields = ["title", "desc", "file_url", "image_url"]
        error_messages = {
            'title': {
                'max_length': '文章标题长度不能低于150',
                'min_length': '文章标题长度不能低于1',
                'required': '文章标题不能为空'
            },
            'desc': {
                'max_length': '文章摘要长度不能低于200',
                'min_length': '文章摘要长度不能低于1',
                'required': '文章摘要不能为空'
            }
        }


class CoursePubForm(forms.ModelForm):
    cover_url = forms.URLField(required=True, error_messages={
        "required": "缩略图地址不能为空",
        "invalid": "不是一个有效的地址"
    })
    video_url = forms.URLField(required=True, error_messages={
        "required": "缩略图地址不能为空",
        "invalid": "不是一个有效的地址"
    })

    teacher = forms.ModelChoiceField(Course.objects.filter(is_delete=False), error_messages={
        "required": "课程讲师不能为空",
    })

    category = forms.ModelChoiceField(Course.objects.filter(is_delete=False), error_messages={
        "required": "课程分类不能为空",
    })

    class Meta:
        model = Course

        fields = ["title", "cover_url", "video_url", "profile", "outline", "teacher", "category"]

        error_messages = {
            "title": {
                "required": "课程标题不能为空"

            },
            "profile": {
                "required": "课程简介不能为空"
            },
            "outline": {
                "required": "课程大纲不能为空"
            }
        }

